package org.example.minebackend.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.minebackend.pojo.*;
import org.example.minebackend.pojo.dto.InvitationCollectPageDTO;
import org.example.minebackend.pojo.dto.InvitationDTO;
import org.example.minebackend.pojo.dto.PersonDTO;
import org.example.minebackend.service.PersonCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("*")
@RequestMapping("/person")
@Slf4j
public class PersonCenterController {
    @Autowired
    private PersonCenterService personCenterService;
    @GetMapping("/recipes")
    public Result getRecipeEntry(@RequestParam Integer userId) {
        log.info("PersonCenterController.getRecipeEntry, 传入参数为userId:{}", userId);
        List<RecipeEntry> result = personCenterService.getRecipeEntry(userId);
        log.info("{}", result);
        return Result.success(result);
    }

    @PostMapping("/invitations")
    public Result getInvitationEntry(@RequestBody InvitationCollectPageDTO invitationCollectPageDTO) {
        log.info("PersonCenterController.getInvitationEntry, userId:{}", invitationCollectPageDTO.getUserId());
        PageBean pageBean = personCenterService.getInvitationEntry(invitationCollectPageDTO);
        return Result.success(pageBean);
    }

    @GetMapping("/invitations/{userId}")
    public Result getInvitationEntryByUserId(@PathVariable Integer userId) {
        log.info("PersonCenterController.getInvitationEntryByUserId, userId:{}", userId);
        List<InvitationDTO> result = personCenterService.getInvitationEntryByUserId(userId);
        return Result.success(result);
    }

    @GetMapping("/message")
    public Result getMessage(@RequestParam Integer id) {
        log.info("获取id:{}的姓名", id);
        User user = personCenterService.getMessage(id);
        return Result.success(user);
    }

    @PutMapping
    public Result updatePersonMessage(@RequestBody PersonDTO personDTO) {
        log.info("更新个人信息：{}", personDTO);
        int status = personCenterService.updatePersonMessage(personDTO);
        if(status == -1) {return Result.error("密码错误，请重新输入密码");}
        return Result.success("更新成功");
    }

    @DeleteMapping("/invitation/{id}")
    public Result deleteInvitation(@PathVariable Integer id) {
        log.info("deleteInvitation, id:{}", id);
        int n = personCenterService.deleteInvitation(id);
        if(n == -1) return Result.error("删除失败");
        return Result.success("删除成功");
    }
}
